package com.sky.cloudbabaservice.service;

import com.sky.cloudbabaservice.convert.StocksConvert;
import com.sky.cloudbabaservice.dao.StocksDao;
import com.sky.cloudbabaservice.entity.Stocks;
import com.sky.cloudbabaserviceapi.bo.StocksBo;
import com.sky.cloudbabaserviceapi.dto.StocksDto;
import com.sky.cloudbabaserviceapi.service.IStocksService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service(version = "1.0.0", timeout = 5000)
public class StocksService implements IStocksService {

    @Resource
    private StocksDao stocksDao;


    @Override
    public StocksBo saveEntity(StocksDto stocksDto) {
        Stocks stocks = StocksConvert.INSTANCE.dto2po(stocksDto);
        Stocks newStocks = stocksDao.save(stocks);
        return StocksConvert.INSTANCE.po2bo(newStocks);
    }

    @Transactional(readOnly = false)
    public String reduceStock(Long goodId, int num) {
        Stocks stocks = stocksDao.getByGoodId(1L);
        long a = 0/0;
        stocks.reduce(num);
        stocksDao.save(stocks);
        return "ok";
    }
}
